package com.suke.czx.modules.tb.controller;

import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import com.suke.czx.modules.tb.entity.TbCategory;
import com.suke.czx.modules.tb.service.TbCategoryService;
import com.suke.czx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;


/**
 * 商品分类表
 *
 * @author developer
 * @email developer@163.com
 * @date 2025-05-14 18:12:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tb/category")
@Api(value = "TbCategoryController", tags = "商品分类表")
public class TbCategoryController extends AbstractController {
    private final TbCategoryService tbCategoryService;

    /**
     * 列表
     */
    @ApiOperation(value = "商品分类表列表")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        LambdaQueryWrapper<TbCategory> queryWrapper = new LambdaQueryWrapper<>();
        final String keyword = mpPageConvert.getKeyword(params);
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.like(TbCategory::getName, keyword);
        }
        IPage<TbCategory> listPage = tbCategoryService.page(mpPageConvert.pageParamConvert(params), queryWrapper);
        return R.ok().setData(listPage);
    }

    @ApiOperation(value = "获取所有商品分类")
    @PostMapping("/getAll")
    public R all() {
        LambdaQueryWrapper<TbCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TbCategory::getId, TbCategory::getName);
        queryWrapper.orderByAsc(TbCategory::getSortOrder);
        return R.ok().setData(tbCategoryService.list(queryWrapper));
    }


    /**
     * 新增商品分类表
     */
    @ApiOperation(value = "新增商品分类表数据")
    @SysLog("新增商品分类表数据")
    @PostMapping()
    public R save(@RequestBody TbCategory tbCategory) {
        tbCategoryService.save(tbCategory);
        return R.ok();
    }


    /**
     * 修改
     */
    @ApiOperation(value = "修改商品分类表数据")
    @SysLog("修改商品分类表数据")
    @PutMapping()
    public R update(@RequestBody TbCategory tbCategory) {
        tbCategoryService.updateById(tbCategory);
        return R.ok();
    }


    /**
     * 删除
     */
    @ApiOperation(value = "删除商品分类表数据")
    @SysLog("删除商品分类表数据")
    @DeleteMapping
    public R delete(@RequestBody TbCategory tbCategory) {
        tbCategoryService.removeById(tbCategory.getId());
        return R.ok();
    }

}
